/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.controls;

import javafx.beans.DefaultProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;

import java.util.Objects;

/**
 * 可滚动面板
 * @author zpy
 * @version 1.0.0s
 */
@DefaultProperty("content")
public class CustomScrollPane extends StackPane {
    private static final String DEFAULT_STYLE_CLASS = "pes-scroll-pane";
    private final SimpleDoubleProperty CONTENT_MIN_HEIGHT_PROPERTY = new SimpleDoubleProperty();
    private final ScrollPane scrollPane = new ScrollPane();

    public CustomScrollPane() {
        this.getStyleClass().add(DEFAULT_STYLE_CLASS);
        scrollPane.setFitToWidth(true);
        scrollPane.setPannable(true);
        CONTENT_MIN_HEIGHT_PROPERTY.set(scrollPane.getHeight()-5);
        scrollPane.heightProperty().addListener((observable,oldValue,newValue)->{
            if(Objects.nonNull(newValue) && newValue.doubleValue() > 100){
                CONTENT_MIN_HEIGHT_PROPERTY.set(newValue.doubleValue() - 5);
            }
        });

        this.getChildren().add(scrollPane);
    }

    public void setContent(Node content) {
        scrollPane.setContent(content);
        if(content instanceof Region){
            ((Region)content).minHeightProperty().bind(CONTENT_MIN_HEIGHT_PROPERTY);
        }
    }

    public Node getContent() {
        return scrollPane.getContent();//this.contentContainer.getChildren().size() == 2 ? this.contentContainer.getChildren().get(1) : null;
    }

}